﻿Imports dooll.Net
Imports Newtonsoft.Json

Public Class tel400page
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.CacheControl = "no-cache"
        Response.Expires = -1
        Dim udo As String = Request.QueryString("do"),
            telno As String = Request.QueryString("telno"),
            callback As String = Request.QueryString("callback"),
            retVal As String = String.Empty,
            tel As dooll.Net.Tel400 = GetTel400(telno)

        For i = 0 To Request.Cookies.Count - 1
            Dim c As HttpCookie = Request.Cookies(i)
            tel.Cookies.Add(New System.Net.Cookie(c.Name, c.Value, "/", "www.c4006.com"))
        Next

        Select Case LCase(udo)
            Case "addext"       '新增分机
                Dim name As String = Request.QueryString("name"),
                    telext As String = Request.QueryString("num"),
                    json As String = tel.AddExt(name, telext)
                retVal = JsonValue(callback, json)
            Case "modifyext"
                Dim name As String = Request.QueryString("name"),
                    numid As String = Request.QueryString("numid"),
                    num As String = Request.QueryString("num"),
                    json As String = tel.ModifyExt(numid, name, num)
                retVal = JsonValue(callback, json)
            Case "delext"       '删除分机
                Dim numid As String = Request.QueryString("numid"),
                    json As String = tel.DelExt(numid)
                retVal = JsonValue(callback, json)
            Case "ext"       '新增分机
                Dim name As String = Request.QueryString("name"),
                    numid As String = Request.QueryString("numid"),
                    json As String = tel.GetExt(numid)
                retVal = JsonValue(callback, json)
            Case "getbind"         '绑定分机
                Dim numid As String = Request.QueryString("numid"),
                    phone As String = Request.QueryString("phone"),
                    json As String = tel.BindPhone(numid, phone)

                retVal = JsonValue(callback, json)

            Case "bind"         '绑定分机
                Dim numid As String = Request.QueryString("numid"),
                    phone As String = Request.QueryString("phone"),
                    json As String
                If String.IsNullOrEmpty(phone) AndAlso Len(phone) < 3 Then
                    json = "{sucess:'false',message:'请填写正确的号码!'}"
                Else
                    json = tel.BindPhone(numid, phone)
                End If

                retVal = JsonValue(callback, json)
            Case "modifybind"         '绑定分机
                Dim numid As String = Request.QueryString("numid"),
                    phone As String = Request.QueryString("phone"),
                    json As String = tel.ModifyBindPhone(numid, phone)

                retVal = JsonValue(callback, json)
            Case "canclbind"
                Dim numid As String = Request.QueryString("numid"),
                    json As String = tel.DelBindPhone(numid)
                retVal = JsonValue(callback, json)
            Case "post"
                Dim url As String = Request.QueryString("url"),
                    data As String = Request.Form("data"),
                    json As String = tel.doPost(url, data)
                retVal = JsonValue(callback, json)
            Case Else
                Dim url As String = Request.QueryString("url"),
                    json As String = tel.doGet(url)
                retVal = JsonValue(callback, json)
        End Select

        For Each c As System.Net.Cookie In tel.Cookies.GetCookies(New System.Uri("http://www.c4006.com"))
            Dim nc As New HttpCookie(c.Name, c.Value)
            c.Expires = DateTime.Now.AddMinutes(5)
            Response.Cookies.Add(nc)
        Next
        tel = Nothing
        Response.Write(retVal)
        Response.End()
    End Sub

    Private Function JsonValue(ByVal name As String, ByVal s As String) As String
        If String.IsNullOrEmpty(name) Then
            Return s
        Else
            Return String.Format("var {0}={1} ;", name, s)
        End If
    End Function

    Private Function GetTel400(ByVal telno As String) As dooll.Net.Tel400
        Return New dooll.Net.Tel400(telno, "PCAUTO1", "PCAUTO")
    End Function
End Class